/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Permissive License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/resources/sharedsource/licensingbasics/permissivelicense.mspx

 ****************************************************************************************/

using System;

namespace GrooveWebServicesV12Helpers
{
	/// <summary>
	/// Represents the VCard of a Groove Contact.
	/// </summary>
	public class VCard
	{
		private GrooveWebServicesV12Helpers.Contact m_Contact;
		private GrooveWebServicesV12Helpers.Context m_Context;

		private GrooveVCardWebService.vCard m_VCard;

		public VCard(GrooveWebServicesV12Helpers.Contact i_Contact, GrooveWebServicesV12Helpers.GrooveVCardWebService.vCard i_VCard)
		{
			m_Contact = i_Contact;
			m_Context = m_Contact.Context;
			
			m_VCard = i_VCard;
		}

		#region VCard Properties And Methods

		public static GrooveVCardWebService.vCard GetRawVCard(string i_IdentityURL, string i_ContactURI, GrooveWebServicesV12Helpers.Context i_Context)
		{
			//  Format for Contact URI:
			//		"grooveIdentity://q7b2dsziki6bnenn4yetv5scpmfmt94n@"
			//
			//	Format for VCard Post URL:
			//		"/GWS/Groove/2.0/vCard/grooveIdentity/q7b2dsziki6bnenn4yetv5scpmfmt94n@"
			//
			string TargetURI = "/GWS/Groove/2.0/vCard/" + i_ContactURI.Replace(":/", "");
			
			GrooveVCardWebService.GrooveVCard VCardWebService = CreateInitializedVCardWebService
				(
				i_IdentityURL,
				TargetURI,
				i_Context
				);

			System.DateTime BeginDateTime = System.DateTime.Now;

			//  Ask service to read vcard
			GrooveWebServicesV12Helpers.GrooveVCardWebService.vCard VCard = VCardWebService.Read();

			if (GrooveWebServicesV12Helpers.Context.TraceEnabled)
			{
				System.TimeSpan TimeSpan = System.DateTime.Now.Subtract(BeginDateTime);
				System.Diagnostics.Debug.WriteLine("VCardWebService::Read, " + TimeSpan.TotalMilliseconds + " ms");
			}

			return VCard;
		}

		public static GrooveVCardWebService.GrooveVCard CreateInitializedVCardWebService(string i_IdentityURL, string i_TargetURI, GrooveWebServicesV12Helpers.Context i_Context)
		{
			//  Create the service
			GrooveVCardWebService.GrooveVCard VCardWebService = new GrooveVCardWebService.GrooveVCard();

			//  Create the Groove SOAP header
			VCardWebService.GrooveRequestHeaderValue = new GrooveVCardWebService.GrooveRequestHeader();

			//  Set appropriate header values - note that any valid identity will suffice for obtaining VCard
			VCardWebService.GrooveRequestHeaderValue.GrooveRequestKey = i_Context.RequestKey;
			VCardWebService.GrooveRequestHeaderValue.GrooveIdentityURL = i_IdentityURL;

			//  Set URL
			VCardWebService.Url = i_Context.Host + i_TargetURI;

			//  Set Timeout
			VCardWebService.Timeout = i_Context.Timeout;

			return VCardWebService;
		}

		public GrooveWebServicesV12Helpers.Contact Contact
		{
			get
			{
				return m_Contact;
			}
		}

		public GrooveWebServicesV12Helpers.Address[] Addresses
		{
			get
			{
				//  Create array list of Addresses
				System.Collections.ArrayList AddressArrayList = new System.Collections.ArrayList();
				foreach (GrooveVCardWebService.Address Address in m_VCard.Addresses)
				{
					GrooveWebServicesV12Helpers.Address NewAddress = new GrooveWebServicesV12Helpers.Address
						(
						this,
						Address
						);

					AddressArrayList.Add(NewAddress);
				}

				//  Return array from array list
				return (GrooveWebServicesV12Helpers.Address[])AddressArrayList.ToArray(typeof(GrooveWebServicesV12Helpers.Address));
			}
		}

		public GrooveWebServicesV12Helpers.Email[] EmailAddresses
		{
			get
			{
				//  Create array list of Emails
				System.Collections.ArrayList EmailArrayList = new System.Collections.ArrayList();
				foreach (GrooveVCardWebService.Email Email in m_VCard.EmailAddresses)
				{
					GrooveWebServicesV12Helpers.Email NewEmail = new GrooveWebServicesV12Helpers.Email
						(
						this,
						Email
						);

					EmailArrayList.Add(NewEmail);
				}

				//  Return array from array list
				return (GrooveWebServicesV12Helpers.Email[])EmailArrayList.ToArray(typeof(GrooveWebServicesV12Helpers.Email));
			}
		}

		public string FullName
		{
			get
			{
				return m_VCard.FullName;
			}
		}

		public GrooveWebServicesV12Helpers.Name Name
		{
			get
			{
				GrooveWebServicesV12Helpers.Name NewName = new GrooveWebServicesV12Helpers.Name
					(
					this,
					m_VCard.Name
					);

				return NewName;
			}
		}

		public string NickName
		{
			get
			{
				return m_VCard.NickName;
			}
		}

		public GrooveWebServicesV12Helpers.Organization Organization
		{
			get
			{
				GrooveWebServicesV12Helpers.Organization NewOrganization = new GrooveWebServicesV12Helpers.Organization
					(
					this,
					m_VCard.Organization
					);

				return NewOrganization;
			}
		}

		public GrooveWebServicesV12Helpers.Phone[] PhoneNumbers
		{
			get
			{
				//  Create array list of PhoneNumbers
				System.Collections.ArrayList PhoneArrayList = new System.Collections.ArrayList();
				foreach (GrooveVCardWebService.Phone Phone in m_VCard.PhoneNumbers)
				{
					GrooveWebServicesV12Helpers.Phone NewPhone = new GrooveWebServicesV12Helpers.Phone
						(
						this,
						Phone
						);

					PhoneArrayList.Add(NewPhone);
				}

				//  Return array from array list
				return (GrooveWebServicesV12Helpers.Phone[])PhoneArrayList.ToArray(typeof(GrooveWebServicesV12Helpers.Phone));
			}
		}

		public string URI
		{
			get
			{
				return m_VCard.Url;
			}
		}

		#endregion

	}
}
